﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Training.Dia1.Data.Entities;

namespace Training.Dia1.Data.Managers
{
    public class CategoriaManager
    {
        public static IList<Categoria>GetList(string filter)
        {
            using (TrainingDia1DbContext ctx = new TrainingDia1DbContext())
            {
                return ctx.Categorias.Where(u => u.Nombre.Contains(filter)).ToList();
            }
        }

        public static Categoria Create(Categoria Categoria)
        {
            using (TrainingDia1DbContext ctx = new TrainingDia1DbContext())
            {
                Categoria CategoriaDB = ctx.Categorias.Add(Categoria);

                ctx.SaveChanges();
                return CategoriaDB;
            }
        }
        public static Categoria Update(Categoria Categoria)
        {
            using (TrainingDia1DbContext ctx = new TrainingDia1DbContext())
            {
                ctx.Entry(Categoria).State = EntityState.Modified;
                ctx.SaveChanges();
                return Categoria;
            }
        }
        public static Categoria GetById(Guid Id)
        {
            using (TrainingDia1DbContext ctx = new TrainingDia1DbContext())
            {
                Categoria CategoriaDB = ctx.Categorias.Find(Id);
                if (CategoriaDB == null)
                {
                    throw new Exception("Categoria not found in data base");
                }
                return ctx.Categorias.Find(Id);
            }
        }
        public static void Delete(Guid Id)
        {
            using (TrainingDia1DbContext ctx = new TrainingDia1DbContext())
            {
                Categoria CategoriaDB = ctx.Categorias.Find(Id);
                if (CategoriaDB == null)
                {
                    throw new Exception("Categoria not found in data base");
                }
                ctx.Categorias.Remove(CategoriaDB);
                ctx.SaveChanges();
            }
        }
    }
}
